#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "DynamicArray.h"

struct Student
{
    char *name;
    int age;
};
typedef struct Student Stu;

//创建
Stu* CreateStu(const char *name, int age)
{
    Stu *s = (Stu*)malloc(sizeof(Stu));
    if(s == NULL)
    {
        printf("student malloc error!\n");
        return NULL;
    }
    s->name = strdup(name);
    s->age = age;
    return s;
}

//销毁
void FreeStu(void *ptr)
{
    Stu *s = (Stu *)ptr;
    free(s->name);
    free(s);
}

//显示
void PrintStu(void *ptr)
{
    Stu *s = (Stu *)ptr;
    printf("学生姓名：%s 年龄：%d\n", s->name, s->age);
}

bool IsNameEqual(void *ptr1, void *ptr2)
{
    Stu *s = (Stu*)ptr1;
    char *name = (char*)ptr2;
    return strcmp(s->name, name) == 0;
}

void PrintStuArray(void *ptr)
{
    DArray *a = (DArray*)ptr;
    printf("班级：=================\n");
    PrintArray(a, PrintStu);
}

typedef struct
{
    char *name; //班级名
    DArray *stuArray;
}Class;

Class *CreateClass(const char *name)
{
    Class *c = (Class *)malloc(sizeof(Class));
    if(c == NULL)
    {
        printf("class malloc error!\n");
        return NULL;
    }
    c->name = strdup(name);
    c->stuArray = InitDArray(10, FreeStu);
    return c;
}

void FreeClass(void *ptr)
{
    Class *c = (Class *)ptr;
    free(c->name);
    FreeDArray(c->stuArray);
}

bool IsClassNameEql(void *ptr1, void *ptr2)
{
    Class *c = (Class *)ptr1;
    char *name = (char*)ptr2;
    return strcmp(c->name, name) == 0;
}

void InsrtStu2Clss(DArray *a, char *ClassName, Stu *s)
{
    Class *c = (Class*)FindByElement(a, ClassName, IsClassNameEql);
    if(c == NULL)
    {
        printf("没找到该班级：%s\n", ClassName);
        return;
    }
    InsertTail(c->stuArray, s);
}

void PrintClass(void *ptr)
{
    Class *c = (Class*)ptr;
    printf("班级名称：%s\n", c->name);
    PrintArray(c->stuArray, PrintStu);
}


int main()
{
#if 0
    DArray *a = InitDArray(10);

    for(int i = 1; i <= 20; i++)
    {
        InsertTail(a, i);
    }

    RemoveByElement(a, 2);
    PrintArray(a);

    FreeDArray(a);
#endif

#if 0
    DArray *a = InitDArray(10, FreeStu);

    InsertTail(a, CreateStu("张三", 20));
    InsertTail(a, CreateStu("李四", 21));
    InsertTail(a, CreateStu("王五", 22));
    PrintArray(a, PrintStu);
    
    Stu *s = (Stu*)FindByElement(a, "王五", IsNameEqual);
    if(s == NULL)
    {
        printf("没找到！\n");
    }
    else
    {
        RemoveByElement(a, s);
        PrintArray(a, PrintStu);
    }

    FindByElement(a, "王五", IsNameEqual);
    FreeDArray(a);
#endif

#if 0
    //动态二维数组
    DArray *a = InitDArray(3, FreeDArray);

    DArray *class267 = InitDArray(10, FreeStu);
    InsertTail(class267, CreateStu("张三", 20));
    InsertTail(class267, CreateStu("李四", 21));

    DArray *class262 = InitDArray(10, FreeStu);
    InsertTail(class262, CreateStu("王五", 20));
    InsertTail(class262, CreateStu("赵六", 21));

    InsertTail(a, class262);
    InsertTail(a, class267);

    PrintArray(a, PrintStuArray);
#endif

#if 1
    DArray *suqian = InitDArray(10, FreeClass);
    InsertTail(suqian, CreateClass("262班"));
    InsertTail(suqian, CreateClass("267班"));

    InsrtStu2Clss(suqian, "262班", CreateStu("张三",20));
    InsrtStu2Clss(suqian, "262班", CreateStu("李四",21));

    InsrtStu2Clss(suqian, "267班", CreateStu("王五",22));
    InsrtStu2Clss(suqian, "267班", CreateStu("赵六",23));

    PrintArray(suqian, PrintClass);
    FreeDArray(suqian);

#endif


    return 0;
}